package com.example.lin.generate;

import com.example.lin.util.ExportUtil;
import com.example.lin.util.IdUtils;
import com.example.lin.util.ImportUtil;
import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @className: GenerateRegex
 * @description:
 * @author: Lin.Gu
 * @date: 2025/5/14
 */
public class GenerateRegex {

    //主数据去重操作 每个编码只需要插入1次
    public static Set<String> validationFormulaCodeSet = new HashSet<>();

    @Test
    public void generateTsSQLPrint() {
        String filePath = "C:\\Users\\181596\\Desktop\\UBI-CE XL\\IT-UBI CE XL正则.xlsx";
        String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.lastIndexOf("."));
        System.out.println("fileName：" + fileName);
        int sheet = 1;
        //模板信息
        String info = "INSERT INTO `t_base_formula_info` (`id`, `project_code`, `validation_formula_code`, `validation_formula_detail`, `vendor_name`, `business_code`, `carrier_service_product`, `final_destination_country`, `final_destination_region`, `erro_message_cn`, `erro_message_en`, `error_code`, `status`, `is_never_accept`, `is_active`, `create_time`, `create_user_code`, `modify_time`, `modify_user_code`)" +
                "VALUES (\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", 'LM', \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", 1, 0, 1, NOW(), '181596', NOW(), '181596');";
        String detail = "INSERT INTO `t_base_formula_rule` (`id`, `vendor_name`,`formula_code`, `rule_code`, `field_name`, `constraints_regex`, `constraints_unit`, `constraints_currency`, `required`, `is_active`, `create_time`, `create_user_code`, `modify_time`, `modify_user_code`)" +
                "VALUES (\"%s\", \"%s\",\"%s\", \"%s\", \"%s\", \"%s\", NULL, NULL, NULL, 1, NOW(), '181596', NOW(), '181596');";

        List<List<Object>> list = ImportUtil.readExcelToList(filePath, sheet);
        List<Object> sqlList = new LinkedList<>();

        System.out.println("list.size()：" + list.size());
        list.forEach(e -> {
            //主参数信息
            String projectCode = String.valueOf(e.get(0));
            String validationFormulaCode = String.valueOf(e.get(1));
            String validationFormulaDetail = String.valueOf(e.get(5));
            String vendorName = String.valueOf(e.get(2));
            String carrierServiceProduct = String.valueOf(e.get(3));
            String finalDestinationCountry = String.valueOf(e.get(4));
            String erroMessageCn = String.valueOf(e.get(7));
            String erroMessageEn = String.valueOf(e.get(8));
            String errorCode = String.valueOf(e.get(9));
            //明细参数
            String ruleCode = String.valueOf(e.get(6));
            String fieldName = String.valueOf(e.get(10));
            String constraintsRegex = String.valueOf(e.get(11));


            //主表数据信息 t_base_formula_info
            if (!validationFormulaCodeSet.contains(validationFormulaCode)) {
                String finalDestinationCountrys = finalDestinationCountry;
                if(finalDestinationCountry.length() != 2){
                    //不等于2 说明是分组
                    finalDestinationCountrys = "";
                }
                //主正则
                String infoValue = String.format(info, IdUtils.getId(), projectCode, validationFormulaCode, validationFormulaDetail, vendorName,
                        carrierServiceProduct, finalDestinationCountrys, finalDestinationCountry, erroMessageCn, erroMessageEn, errorCode);
                sqlList.add(infoValue);

                validationFormulaCodeSet.add(validationFormulaCode);
            }
            //明细表正则 t_base_formula_rule
            String regexValue = constraintsRegex.replace("\\", "\\\\");
            String detailValue = String.format(detail, IdUtils.getId(), vendorName, validationFormulaCode, ruleCode, fieldName, regexValue);
            sqlList.add(detailValue);
        });

        ExportUtil.writeDataToFile(sqlList, fileName + sheet);
        System.out.println("完成");
    }


}
